import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '../vuex/store';
import Util from '../common/util';
import iView from 'iview';
import Cookies from 'js-cookie';
import i18n from '../components/i18n/i18n.js';

Vue.use(VueRouter);


let thisCurrentLanguage=localStorage.getItem('currentLanguage');

console.log();

let routerTitle={};

if(thisCurrentLanguage=='zh'){
	routerTitle.systemTitle='系统概览';
	routerTitle.pageNotFound='404-页面不存在';
	routerTitle.InsufficientPermissions="401-权限不足";
	routerTitle.serviceError="500-服务端错误";
	routerTitle.index="首页";
	routerTitle.DataDictionary="数据字典";
	routerTitle.MemberManagement="成员管理";
	routerTitle.DepartmentManagement="部门管理";
	routerTitle.ResourceMenu="资源菜单";
	routerTitle.ResourceRightsManagement="资源权限管理";
	routerTitle.accountSettings="账户设置";
	routerTitle.SingleSettings="单客服设置";
	routerTitle.MultipleSettings="多客服设置";
	routerTitle.PictureManagement="图片管理";
	routerTitle.completeInfo="补全商户申请信息";
	routerTitle.AddItem="新增商品";
	routerTitle.ProductList="商品列表";
	routerTitle.ProductEditor="商品编辑";
	routerTitle.ProductReviews="商品评论";
	routerTitle.CouponManagement="优惠券管理";
	routerTitle.CouponEditing="优惠券编辑";
	routerTitle.CouponCollectionRecord="优惠券领取记录";
	routerTitle.BasicCategoryList="基础类目列表";
	routerTitle.MerchantFundsFlow="商户资金流水";
	routerTitle.MerchantRefundForm="商户退款单";
	routerTitle.RefundDetails="退款单详情";
	routerTitle.MerchantRequest="商户提现申请";
	routerTitle.MerchantOrder="商户订单";
	routerTitle.BusinessInfo="商户信息";
	routerTitle.ShippingTemplate="商户运费模板";
	routerTitle.userList="用户列表";
	routerTitle.LoginLog="登录日志";
	routerTitle.BusinessOverview="商户概览";
	routerTitle.systemInfo="系统消息";
	routerTitle.MerchantOrderDetails="商户订单详情";
	routerTitle.OverviewAccounts="财务概览";
}else if(thisCurrentLanguage=='en'){
	routerTitle.systemTitle='System overview';
	routerTitle.pageNotFound='404-Page does not exist';
	routerTitle.InsufficientPermissions="401-Insufficient permissions";
	routerTitle.serviceError="500-Server error";
	routerTitle.index="Home";
	routerTitle.DataDictionary="Data Dictionary";
	routerTitle.MemberManagement="Member management";
	routerTitle.DepartmentManagement="Department management";
	routerTitle.ResourceMenu="Resource menu";
	routerTitle.ResourceRightsManagement="Resource rights management";
	routerTitle.accountSettings="account settings";
	routerTitle.SingleSettings="Single customer service settings";
	routerTitle.MultipleSettings="Multiple customer service settings";
	routerTitle.PictureManagement="Picture management";
	routerTitle.completeInfo="Completion of merchant application information";
	routerTitle.AddItem="Add item";
	routerTitle.ProductList="Product list";
	routerTitle.ProductEditor="Product editor";
	routerTitle.ProductReviews="Product Reviews";
	routerTitle.CouponManagement="Coupon management";
	routerTitle.CouponEditing="Coupon editing";
	routerTitle.CouponCollectionRecord="Coupon collection record";
	routerTitle.BasicCategoryList="Basic category list";
	routerTitle.MerchantFundsFlow="Merchant funds flow";
	routerTitle.MerchantRefundForm="Merchant refund form";
	routerTitle.RefundDetails="Refund details";
	routerTitle.MerchantRequest="Merchant withdrawal request";
	routerTitle.MerchantOrder="Merchant order";
	routerTitle.BusinessInfo="Business information";
	routerTitle.ShippingTemplate="Merchant Shipping Template";
	routerTitle.userList="user list";
	routerTitle.LoginLog="Login log";
	routerTitle.BusinessOverview="Business overview";
	routerTitle.systemInfo="system information";
	routerTitle.MerchantOrderDetails="Merchant order details";
	routerTitle.OverviewAccounts="Overview of accounts";
}else if(thisCurrentLanguage=='it'){
	routerTitle.systemTitle='System overview';
	routerTitle.pageNotFound='404-Page does not exist';
	routerTitle.InsufficientPermissions="401-Insufficient permissions";
	routerTitle.serviceError="500-Server error";
	routerTitle.index="Home";
	routerTitle.DataDictionary="Data Dictionary";
	routerTitle.MemberManagement="Member management";
	routerTitle.DepartmentManagement="Department management";
	routerTitle.ResourceMenu="Resource menu";
	routerTitle.ResourceRightsManagement="Resource rights management";
	routerTitle.accountSettings="account settings";
	routerTitle.SingleSettings="Single customer service settings";
	routerTitle.MultipleSettings="Multiple customer service settings";
	routerTitle.PictureManagement="Picture management";
	routerTitle.completeInfo="Completion of merchant application information";
	routerTitle.AddItem="Add item";
	routerTitle.ProductList="Product list";
	routerTitle.ProductEditor="Product editor";
	routerTitle.ProductReviews="Product Reviews";
	routerTitle.CouponManagement="Coupon management";
	routerTitle.CouponEditing="Coupon editing";
	routerTitle.CouponCollectionRecord="Coupon collection record";
	routerTitle.BasicCategoryList="Basic category list";
	routerTitle.MerchantFundsFlow="Merchant funds flow";
	routerTitle.MerchantRefundForm="Merchant refund form";
	routerTitle.RefundDetails="Refund details";
	routerTitle.MerchantRequest="Merchant withdrawal request";
	routerTitle.MerchantOrder="Merchant order";
	routerTitle.BusinessInfo="Business information";
	routerTitle.ShippingTemplate="Merchant Shipping Template";
	routerTitle.userList="user list";
	routerTitle.LoginLog="Login log";
	routerTitle.BusinessOverview="Business overview";
	routerTitle.systemInfo="system information";
	routerTitle.MerchantOrderDetails="Merchant order details";
	routerTitle.OverviewAccounts="Panoramica finanziaria";
}


let router_project_name = Util.projectName();

/**
 * 定义基础路由
 */
const page404 = {
    path: router_project_name + '/*',
    name: 'error_404',
    meta: {
        title: routerTitle.pageNotFound
    },
    component: resolve => { require(['../pages/basic/error_page/404.vue'], resolve); }
};

const page401 = {
    path: router_project_name + '/401',
    meta: {
        title: routerTitle.InsufficientPermissions
    },
    name: 'error_401',
    component: resolve => { require(['../pages/basic/error_page/401.vue'], resolve); }
};

const page500 = {
    path: router_project_name + '/500',
    meta: {
        title: routerTitle.serviceError
    },
    name: 'error_500',
    component: resolve => { require(['../pages/basic/error_page/500.vue'], resolve); }
};

const locking = {
    path: router_project_name + '/locking',
    name: 'locking',
    component: resolve => { require(['../pages/basic/locking-page/locking-page.vue'], resolve); }
};
/**
 * 定义基础路由
 */
const basicRoute = {
	path: '/' + router_project_name,
	component: (resolve) => require(['../pages/framework/index.vue'], resolve),
	meta: {
		title: routerTitle.index
	},
	children: [
		{
			path: router_project_name + '/demo',
			component: (resolve) => require(['../pages/demo/demo.vue'], resolve),
		},
		{
			path: router_project_name + '/demo/new',
			component: (resolve) => require(['../pages/demo/demo1.vue'], resolve),
		},
		{
			path: router_project_name + '/datadict',
			component: (resolve) => require(['../pages/basic/datadict/list.vue'], resolve),
			name: 'sys_datadict_list',
			meta: {
				title: routerTitle.DataDictionary
			}
		},
		{
			path: router_project_name + '/user',
			component: (resolve) => require(['../pages/basic/user/list.vue'], resolve),
			name: 'sys_user_list',
			meta: {
				title: routerTitle.MemberManagement
			}
		},
		{
			path: router_project_name + '/role',
			component: (resolve) => require(['../pages/basic/role/list.vue'], resolve),
			name: 'sys_role_list',
			meta: {
				title: routerTitle.DepartmentManagement
			}
		},
		{
		 	path: router_project_name + '/resource',
		 	component: (resolve) => require(['../pages/basic/resource/list.vue'], resolve),
		 	name: 'sys_resource_list',
		 	meta: {
				title: routerTitle.ResourceMenu
			}
		},
		{
		 	path: router_project_name + '/resource/perms/:resourceId/:rsv_',
		 	component: (resolve) => require(['../pages/basic/resource/permission.vue'], resolve),
		 	name: 'sys_resource_permission',
		 	meta: {
				title: routerTitle.ResourceRightsManagement
			}
		},		
		{
		 	path: router_project_name + '/profile',
		 	component: (resolve) => require(['../pages/basic/profile/index.vue'], resolve),
		 	name: 'sys_account_profile',
		 	meta: {
				title: routerTitle.accountSettings
			}
		},
		{
		 	path: router_project_name + '/im/single',
		 	component: (resolve) => require(['../pages/plugins/im-service/single/index.vue'], resolve),
		 	name: 'sys_single_im',
		 	meta: {
				title: routerTitle.SingleSettings
			}
		},
		{
		 	path: router_project_name + '/im/multi',
		 	component: (resolve) => require(['../pages/plugins/im-service/multi/list.vue'], resolve),
		 	name: 'sys_multi_im',
		 	meta: {
				title: routerTitle.MultipleSettings
			}
		},
		{
		 	path: router_project_name + '/imgs',
		 	component: (resolve) => require(['../pages/basic/imgs/index.vue'], resolve),
		 	name: 'sys_imgs',
		 	meta: {
				title: routerTitle.PictureManagement
			}
		},
		{
		 	path: router_project_name + '/index',
		 	component: (resolve) => require(['../pages/ms/index/index.vue'], resolve),
		 	name: 'sys_index',
		 	meta: {
				title: routerTitle.systemTitle
			}
		}
	]
};

/**
 * 业务路由配置
 */
const busRoute = {
	path: '/' + router_project_name,
	component: (resolve) => require(['../pages/framework/index.vue'], resolve),
	meta: {
		title: routerTitle.index,
		name: ''
	},
	children: [
		{
			path: router_project_name + '/incompletemerchants',
			component: (resolve) => require(['../pages/ms/merchant/incompletemerchants.vue'], resolve),
			name: 'ms_incomplete_merchant',
			meta: {
			   title: routerTitle.completeInfo
		   }
	   },
		{
		 	path: router_project_name + '/v2/addproduct',
		 	component: (resolve) => require(['../pages/ms/productv2/product.vue'], resolve),
		 	name: 'ms_add_product_v2',
		 	meta: {
				title: routerTitle.AddItem
			}
		},
		{
		 	path: router_project_name + '/v2/products',
		 	component: (resolve) => require(['../pages/ms/productv2/index.vue'], resolve),
		 	name: 'ms_product_list_v2',
		 	meta: {
				title: routerTitle.ProductList
			}
		},
		{
		 	path: router_project_name + '/v2/products/edit',
		 	component: (resolve) => require(['../pages/ms/productv2/editProduct.vue'], resolve),
		 	name: 'ms_edit_product_v2',
		 	meta: {
				title: routerTitle.ProductEditor
			}
		},
		{
		 	path: router_project_name + '/product/productReview',
		 	component: (resolve) => require(['../pages/ms/productv2/productReview.vue'], resolve),
		 	name: 'ms_product_productReview',
		 	meta: {
				title: routerTitle.ProductReviews
			}
		},
		{
		 	path: router_project_name + '/coupon',
		 	component: (resolve) => require(['../pages/ms/coupon/list.vue'], resolve),
		 	name: 'ms_coupon_list',
		 	meta: {
				title: routerTitle.CouponManagement
			}
		},
		{
		 	path: router_project_name + '/coupon/edit',
		 	component: (resolve) => require(['../pages/ms/coupon/edit.vue'], resolve),
		 	name: 'ms_coupon_edit',
		 	meta: {
				title: routerTitle.CouponEditing
			}
		},
		{
		 	path: router_project_name + '/coupon/record',
		 	component: (resolve) => require(['../pages/ms/coupon/record.vue'], resolve),
		 	name: 'ms_coupon_record',
		 	meta: {
				title: routerTitle.CouponCollectionRecord
			}
		},
		{
		 	path: router_project_name + '/productCategory',
		 	component: (resolve) => require(['../pages/ms/productCategory/list.vue'], resolve),
		 	name: 'ms_productCategory_list',
		 	meta: {
				title: routerTitle.BasicCategoryList
			}
		},
		//纪文亭
		{
			path: router_project_name + '/merchantAccount',
			component: (resolve) => require(['../pages/ms/merchantAccount/list.vue'], resolve),
			name: 'ms_merchantAccount_list',
			meta: {
				title: routerTitle.MerchantFundsFlow
			}
		},
		{
			path: router_project_name + '/merchantRefund',
			component: (resolve) => require(['../pages/ms/merchantRefund/list.vue'], resolve),
			name: 'ms_merchantRefund_list',
			meta: {
				title: routerTitle.MerchantRefundForm
			}
		},
		{
			path: router_project_name + '/merchantRefundDetail',
			component: (resolve) => require(['../pages/ms/merchantRefund/detail.vue'], resolve),
			name: 'ms_merchantRefundDetail_list',
			meta: {
				title: routerTitle.RefundDetails
			}
		},
		{
			path: router_project_name + '/merchantWithdraw',
			component: (resolve) => require(['../pages/ms/merchantWithdraw/list.vue'], resolve),
			name: 'ms_merchantWithdraw_list',
			meta: {
				title: routerTitle.MerchantRequest
			}
		},
		{
			path: router_project_name + '/order',
			component: (resolve) => require(['../pages/ms/order/list_new.vue'], resolve),
			name: 'ms_order_list',
			meta: {
				title: routerTitle.MerchantOrder
			}
		},	
		{
			path: router_project_name + '/merchant',
			component: (resolve) => require(['../pages/ms/merchant/list.vue'], resolve),
			name: 'ms_merchant_list',
			meta: {
				title: routerTitle.BusinessInfo
			}
		},
		{
			path: router_project_name + '/merchantFreightTemplates',
			component: (resolve) => require(['../pages/ms/merchantFreightTemplates/list.vue'], resolve),
			name: 'ms_merchantFreightTemplates_list',
			meta: {
				title: routerTitle.ShippingTemplate
			}
		},
		{
			path: router_project_name + '/merchantMember',
			component: (resolve) => require(['../pages/ms/merchantMember/list.vue'], resolve),
			name: 'ms_merchantMember_list',
		 	meta: {
				title: routerTitle.userList
			}
		},
		{
			path: router_project_name + '/loginLog',
			component: (resolve) => require(['../pages/ms/loge/loginList.vue'], resolve),
			name: 'ms_loginLog_list',
			meta: {
				title: routerTitle.LoginLog
			}
		},
		{
			path: router_project_name + '/dashboard',
			component: (resolve) => require(['../pages/ms/orderCensus/list.vue'], resolve),
			name: 'ms_dashboard_list',
			meta: {
				title: routerTitle.BusinessOverview
			}
		},
		{
			path: router_project_name + '/messagesms/smsList',
			component: (resolve) => require(['../pages/ms/messagesms/smsList.vue'], resolve),
			name: 'ms_messagesms_smsList',
		 	meta: {
				title: routerTitle.systemInfo
			}
		},
		// 财务概览
		{
			path: router_project_name + '/overviewAccounts',
			component: (resolve) => require(['../pages/ms/overviewAccounts/list.vue'],resolve),
			name:'ms_overviewAccounts_list',
			meta: {
				title: routerTitle.OverviewAccounts
			}
		},
		// 添加收款账户
		{	
			path: router_project_name + '/overviewAccounts/addAccount',	
			component: (resolve) => require(['../pages/ms/overviewAccounts/addAccount.vue'],resolve),	
			name:'ms_addAccount_list',
			meta: {
				title: '收款账户'
			}
		},
		// 提现记录
		{
			path: router_project_name + '/drawRecord',
			component: (resolve) => require(['../pages/ms/drawRecord/list.vue'],resolve),
			name:'ms_drawRecord_list',
			meta: {
				title: '提现记录'
			}
		}
	]	
};
const busRoute1 = [{
	path: router_project_name + '/demo2',
	component: (resolve) => require(['../pages/demo/demo2.vue'], resolve)
}, {
	path: router_project_name + '/orderDetail',
	component: (resolve) => require(['../pages/ms/order/detail.vue'], resolve),
	name: 'ms_orderDetail_detail',
	meta: {
		title: routerTitle.MerchantOrderDetails
	}
}];




/**
 * auth true 登录才能访问的页面
 * name 为命名路由,建议不要用于显示中文名称(可以为CODE,由服务端控制显示名称)
 * displayName 为测试新加属性
 */
let routes = [
	locking,
	basicRoute,
	busRoute,
	...busRoute1,
	page500,
	page401,
	page404
];


const router = new VueRouter({
	mode: 'history',
    routes:routes
})

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start();
	Util.title(to.meta.title);
	console.log(to.meta.title);

    if (Cookies.get('locking') === '1' && to.name !== 'locking') {  // 判断当前是否是锁定状态
    	iView.LoadingBar.finish();
    	next(false);
    	router.push({
    		name: 'locking'
    	});
    } else if (Cookies.get('locking') === '0' && to.name === 'locking') {
    	iView.LoadingBar.finish();
    	next(false);
    } else {
    	//1.处理第一次加载
    	let isFirstLoad = store.getters.framework_firstLoad;
    	if(isFirstLoad) {
    		store.dispatch('authUpdateMenu', {'router': router, 'curr': to});	
    		store.dispatch('frameworkUpdateFirstLoad');
    	}
    	next();
	}
});

router.afterEach(transition => {
    iView.LoadingBar.finish();
    window.scrollTo(0, 0);
});


export default router;